//
//  NearbyMotionViewController.m
//  Finance
//
//  Created by cheng xiang on 13-7-5.
//  Copyright (c) 2013年 cheng xiang. All rights reserved.
//

#import "NearbyMotionViewController.h"
#import "NearbyMotionView.h"
#import "AppDelegate.h"
#import "NearbyTableViewController.h"
#import "AnalysisJson.h"
#import "MultiThreading.h"

@interface NearbyMotionViewController ()

@end

@implementation NearbyMotionViewController
@synthesize locationManager = _locationManager, locationDic = _locationDic;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (BOOL)canResignFirstResponder
{
    return YES;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"toolbar.jpg"] forBarMetrics:UIBarMetricsDefault];
    }
    
    NearbyMotionView *motionView = [[NearbyMotionView alloc] initWithFrame:self.view.bounds];
    [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
    //motionView.userInteractionEnabled = NO;
    [self.view addSubview:motionView];
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [self setHidesBottomBarWhenPushed:NO];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self setHidesBottomBarWhenPushed:NO];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidAppear:(BOOL)animated
{
    [self becomeFirstResponder];
}

//摇一摇功能
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.subtype == UIEventSubtypeMotionShake) {
        //摇一摇具体实现 add by chengxiang 20130407
        NSLog(@"fuck!!stop shaking");
        
        [self packageRequest];
        
    }
}

- (void)getCurrentLocation
{
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        [_locationManager startUpdatingLocation];
    }else{
        [_locationManager startUpdatingLocation];
    }
    
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [_locationManager stopUpdatingLocation];
    if (_locationDic == nil) {
        _locationDic = [[NSMutableDictionary alloc] init];
    }
    NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
    NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
    _locationDic[LATITUDE] = strLat;
    _locationDic[LONGITUDE] = strLng;
    NSLog(@"Lat: %@  Lng: %@", strLat, strLng);
    [self packageRequest];
    
}

- (void)packageRequest
{
    //组请求报文
    NSDictionary *header = [AnalysisJson PackageJsonHeader:@"W120"];
    NSDictionary *body = [self PackageBody];
    NSData *requestJson = [AnalysisJson PackageJsonFromHeader:header andBody:body];
    
    //发送报文
    [self getTableData: requestJson];
}

//每次组报文需要实现类似组body的方法
- (NSDictionary *)PackageBody:(NSString *)longitude lat:(NSString *)latitude
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"longitude"] = longitude;
    dic[@"latitude"] = latitude;
    return dic;
}

- (NSDictionary *)PackageBody
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    return dic;
}

- (void)getTableData:(NSData *)data
{
    [MultiThreading getDataFromNetwork:[SERVICE_URL stringByAppendingString:@"QueryProdByShake"] toObject:self callBack:@selector(requestCallBack:) withBody:data];
}

- (void)requestCallBack:(NSDictionary *)rdic
{
    NSObject *body = rdic[@"body"];
    NSArray *array = nil;
    if([body isKindOfClass:[NSDictionary class]] && [(NSDictionary *)body objectForKey:@"products"] != nil)
    {
        array = [(NSDictionary *)body objectForKey:@"products"];
    }
    
    [self setHidesBottomBarWhenPushed:YES];
    NearbyTableViewController *aController = [[NearbyTableViewController alloc] init];
    aController.array = array;
    [self.navigationController pushViewController:aController animated:YES];
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
